/********************************************************************************************
** Copyright � 2011 Nokia Corporation. All rights reserved.
** Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
** Java and all Java-based marks are trademarks or registered trademarks of
** Sun Microsystems, Inc. Other product and company names mentioned herein may be
** trademarks or trade names of their respective owners.
**
** Subject to the conditions below, you may, without charge:
**
** �  Use, copy, modify and/or merge copies of this software and
**    associated content and documentation files (the �Software�)
**
** �  Publish, distribute, sub-licence and/or sell new software
**    derived from or incorporating the Software.
**
** Some of the documentation, content and/or software maybe licensed under open source
** software or other licenses.  To the extent such documentation, content and/or
** software are included, licenses and/or other terms and conditions shall apply
** in addition and/or instead of this notice. The exact terms of the licenses, disclaimers,
** acknowledgements and notices are reproduced in the materials provided.
**
** This file, unmodified, shall be included with all copies or substantial portions
** of the Software that are distributed in source code form.
**
** The Software cannot constitute the primary value of any new software derived
** from or incorporating the Software.
**
** Any person dealing with the Software shall not misrepresent the source of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
********************************************************************************************/
import Qt 4.7

import "DCDataClasses.js" as DCDATA

Rectangle
{
    id: carpoolactivationdlg
    width: 300
    height: 110
    color: "#eef185"
    radius: 10

    property string datacloudurl: ""
    property string carpoolkarpoid: "0"
    property alias showalias: aliastextinput.text
    property string activationstate: "0"

    signal startSendingGPS(string carpoolkarpoid, string showalias)
    signal stopSendingGPS()

    z: 50

    function initAndShow(useralias, currentcarpoolcarpoid)
    {
        carpoolkarpoid = currentcarpoolcarpoid
        showalias = useralias
        carpoolactivationdlg.visible = true
        activationtext.text = "[not activated]"
        activationstate = "0"

        var carpoolkarpoidarr = []
        carpoolkarpoidarr[0] = carpoolkarpoid

        var cpcarpoolactivationlist = new DCDATA.DCCarpoolActivationsList()
        var cpcarpoolactivation = new DCDATA.DCCarpoolActivation()
        cpcarpoolactivation.carpoolkarpoid = carpoolkarpoid
        cpcarpoolactivationlist.carpoolactivationlist[0] = cpcarpoolactivation
        DCDATA.send(cpcarpoolactivationlist,datacloudurl,"getcarpoolactivation",getcarpoolsactivationcb)
    }

    function getcarpoolsactivationcb(response)
    {
        console.log("CarpoolActivation::getcarpoolsactivationcb: " + JSON.stringify(response))

        try
        {
            if (response.carpoolactivationlist.length > 0)
            {
                showalias = response.carpoolactivationlist[0].alias
                activationstate = response.carpoolactivationlist[0].active
                if (activationstate == "1")
                {
                    activationtext.text = "[activated]"
                }
            }
        }
        catch (e)
        {
        }
    }


    Text {
        id: aliaslabeltext
        width: 70
        height: 20
        text: "Use alias:"
        font.family: "Arial"
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.top: titletext.bottom
        anchors.topMargin: 12
        font.pixelSize: 14
    }

    function carpoolactivationcb(response)
    {
        console.log("CarpoolActivation::carpoolactivationcb: " + response.statusString)

        if (!response.isError)
        {
            carpoolactivationdlg.visible = false
            if (activationstate == "1")
            {
                carpoolactivationdlg.startSendingGPS(carpoolkarpoid, showalias)
            }
            else
            {
                carpoolactivationdlg.stopSendingGPS()
            }
        }
        else
        {
            messagebox.text = "Failed: " + response.errorString
        }
    }

    PushButton {
        id: gpsonbutton
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        label: "GPS on"

        onButtonpressed:
        {
            activationstate = "1"
            var cpcarpoolactivationlist = new DCDATA.DCCarpoolActivationsList()
            var cpcarpoolactivation = new DCDATA.DCCarpoolActivation()
            cpcarpoolactivation.carpoolkarpoid = carpoolkarpoid
            cpcarpoolactivation.active = activationstate
            cpcarpoolactivation.lat = "0"
            cpcarpoolactivation.lon = "0"
            cpcarpoolactivationlist.carpoolactivationlist[0] = cpcarpoolactivation
            DCDATA.send(cpcarpoolactivationlist,datacloudurl,"setcarpoolactivation",carpoolactivationcb)
        }
    }



    PushButton {
        id: gpsoffbutton
        anchors.left: gpsonbutton.right
        anchors.leftMargin: 10
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        label: "GPS off"

        onButtonpressed:
        {
            activationstate = "0"
            var cpcarpoolactivationlist = new DCDATA.DCCarpoolActivationsList()
            var cpcarpoolactivation = new DCDATA.DCCarpoolActivation()
            cpcarpoolactivation.carpoolkarpoid = carpoolkarpoid
            cpcarpoolactivation.active = activationstate
            cpcarpoolactivation.lat = "0"
            cpcarpoolactivation.lon = "0"
            cpcarpoolactivationlist.carpoolactivationlist[0] = cpcarpoolactivation
            DCDATA.send(cpcarpoolactivationlist,datacloudurl,"setcarpoolactivation",carpoolactivationcb)
        }
    }

    PushButton {
        id: closebutton
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        label: "Close"

        onButtonpressed:
        {
            carpoolactivationdlg.visible = false
        }
    }


    Text {
        id: titletext
        width: 80
        height: 20
        text: "Carpool activation"
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.top: parent.top
        anchors.topMargin: 10
        font.family: "Arial"
        font.pixelSize: 16
    }

    Rectangle {
        id: rectangle1
        height: 22
        color: "#ffffff"
        border.color: "#000000"
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.left: aliaslabeltext.right
        anchors.leftMargin: 0
        anchors.top: titletext.bottom
        anchors.topMargin: 10

        TextInput {
            id: aliastextinput
            text: ""
            anchors.right: parent.right
            anchors.rightMargin: 0
            anchors.left: parent.left
            anchors.leftMargin: 3
            anchors.verticalCenter: parent.verticalCenter
            font.pixelSize: 12
        }
    }

    Text {
        id: activationtext
        x: 257
        width: 80
        height: 20
        text: "[not activated]"
        horizontalAlignment: Text.AlignRight
        anchors.top: parent.top
        anchors.topMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 10
        font.family: "Arial"
        font.pixelSize: 12
    }
}
